/** 
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
* 
* PmPurchaseApplyService
* 外包申请
* 
* 修改纪录
* 2020-05-09 版本：1.0 shenyouyi 创建。
* @version 版本：1.0
* @author 作者：shenyouyi
* 创建日期：2020-05-09
*/
package com.topscomm.pm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.pm.pojo.PmPurchaseApplyEntity;
import com.topscomm.pm.service.IPmProjectService;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.pm.service.IPmPurchaseApplyService;
import com.topscomm.pm.service.impl.auto.PmPurchaseApplyServiceAuto;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(value = "pmPurchaseApplyService")
@Scope(value = "singleton")
public class PmPurchaseApplyService extends PmPurchaseApplyServiceAuto implements IPmPurchaseApplyService {

    @Autowired
    private IPmProjectService pmProjectService;

    /**
     * @author: shenyouyi
     * @date: 2020-05-11
     * @param entity
     * @description:单号生成
     * @modify:
     */
    @Override
    protected void beforeInsert(BasicEntity entity){
        super.beforeInsert(entity);
        if (entity.getId() <= 0) {// 赋值Id和单号
            long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
            entity.setId(arrayId[0]);
            String docNo = String.valueOf(arrayId[0]);
            docNo = docNo.substring(0, 6) + docNo.substring(9);
            entity.setValue(PmPurchaseApplyEntity.FieldDocno, docNo);
        }
    }

    /**
      * @author: shenyouyi
      * @date: 2020/6/16
      * @param
      * @description: 采购申请审批通过或者取消审批后执行的方法
      **/
    @Override
    public void updatePurchasemoneyOfProjectBase(String sourceId, boolean approvalSuccess) {
        PmPurchaseApplyEntity pmPurchaseApplyEntity = this.queryById(sourceId);
        // 1.查询出去除本条申请单，审批通过的采购金额总和
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("projectid = {0} and state = {1} and id <> {2}", pmPurchaseApplyEntity.getProjectid(), TapSystemConst.TapApprovalBillStateEnum.Approved, sourceId);
        List<String> queryFields = new ArrayList<>();
//        queryFields.add("sum(applymoney) as totalmoney");
        queryFields.add("sum(NVL(applymoney,0)) as totalmoney");
        BigDecimal totalMoney = BigDecimal.ZERO;
        if(approvalSuccess){
            totalMoney = pmPurchaseApplyEntity.getApplymoney();
        }
        List<Map<String,Object>> applyMoneyList = this.queryMapFieldsByWhere(sql.toString(), queryFields);
        if(applyMoneyList.size()>0 && applyMoneyList.get(0)!=null){
            BigDecimal partMoney =  (BigDecimal) applyMoneyList.get(0).get("totalmoney");
            totalMoney = totalMoney.add(partMoney);
        }
        // 更新项目表中的预算值
        Map<String,Object> updateFields = new HashMap<>();
        updateFields.put("purchasemoney", totalMoney);
        sql.clear();
        sql.appendSingle("id = {0}", pmPurchaseApplyEntity.getProjectid());
        pmProjectService.updateByWhere(updateFields, sql.toString());

    }
}
